/**
 * 防抖节流
 * @author 木人心
 * @version 1.0.0
 */
export default {
  /**
   * 防抖 - 延迟触发,多次调用触发最后一次
   * @param fn
   * @param delay
   */
  debounce (fn: any, delay = 500) {
    let timer: any
    return function (...arg) {
      if (timer) {
        clearTimeout(timer)
      }
      timer = setTimeout(() => {
        fn(...arg)
      }, delay)
    }
  },

  /**
   * 节流 - 一段时间内只允许触发一次
   * @param fn
   * @param delay
   */
  throttle (fn: any, delay = 800) {
    let timer: any
    return function (...arg) {
      if (!timer) {
        fn(...arg)
        timer = setTimeout(() => {
          clearTimeout(timer)
          timer = null
        }, delay)
      }
    }
  }
}
